package com.creating.mict.action;



import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.creating.mict.common.DateUtils;
import com.creating.mict.domain.Office;
import com.creating.mict.domain.Patient;
import com.creating.mict.service.IColumnService;
import com.creating.mict.service.IOfficeService;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class OfficeAction extends ActionSupport {

	@Resource
	private IOfficeService officeService;
	
	
//	//获取所有机构树
//	public String getAllOfficeTree() {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		//或当前登录用户名
//		String loginName = (String)request.getSession().getAttribute("loginName");
//		String str = officeService.buildCheckOfficeTreeByUser(node, fcode);//buildCheckReportTree(node);
//		request.setAttribute("result", str);
//		return "success";
//	}
	
	
	/**
	 * 
	* @Title: buildCheckOfficeTree 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String buildCheckOfficeTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		if(node != null && node.equals("-1")) {
			node = loginOffice;
		}
		String str = officeService.buildCheckOfficeTree(node);
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	 * 
	* @Title: buildOfficeTree 
	* @Description: 普通单选树
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String buildOfficeTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		if(node != null && node.equals("-1")) {
			node = loginOffice;
		}
		String str = officeService.buildOfficeTree(node);
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	 * 
	* @Title: queryOfficeList 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryOfficeList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Office> list = officeService.queryOfficeList(fparent);
		if(list == null) {
			total = 0;
		} else {
			total = list.size();
		}
		StringBuffer jsonStr = new StringBuffer("[");
		if (start + limit > total)
			limit = total - start;

		for (int i = 0; i < total; i++) {

			Office newOffice = (Office) list.get(i);

			jsonStr.append("{");
			jsonStr.append("fid:'").append(newOffice.getFid()).append("',");
			jsonStr.append("fcode:'").append(newOffice.getFcode()).append("',");
			jsonStr.append("fname:'").append(replaceBlank(newOffice.getFname())).append("',");
			jsonStr.append("fadd:'").append(replaceBlank(newOffice.getFadd())).append("',");
			jsonStr.append("fadmin:'").append(newOffice.getFadmin()).append("',");
			jsonStr.append("fadtel:'").append(newOffice.getFadtel()).append("',");
			jsonStr.append("ftel:'").append(newOffice.getFtel()).append("',");
			jsonStr.append("femail:'").append(newOffice.getFemail()).append("',");
			
			jsonStr.append("fxzqh:'").append(newOffice.getFxzqh()).append("',");
			jsonStr.append("ftype:'").append(newOffice.getFtype()).append("',");
			jsonStr.append("fleaf:'").append(newOffice.getFleaf()).append("',");
			jsonStr.append("fparent:'").append(newOffice.getFparent()).append("',");
			jsonStr.append("fstate:'").append(newOffice.getFstate()).append("',");
			jsonStr.append("forder:'").append(newOffice.getForderStr()).append("',");
			
			jsonStr.append("fbz:'").append(newOffice.getFbz()).append("'");

			if (i == total - 1) {
				jsonStr.append("}");
			} else {
				jsonStr.append("},");
			}
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + total + ", root:"
				+ jsonStr.toString() + "}";
		
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	* @Title: deleteOffice 
	* @Description: 删除机构
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteOffice() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		boolean boo1 = officeService.deleteOfficeByCode(fcode);
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: saveOffice 
	* @Description: 保存机构
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String saveOffice() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Office offi = new Office();
		String temp = "";
		if(fcode == null || fcode.equals("null") || fcode.equals("")) {
			temp = DateUtils.getTransactionIdByLongTime();
			offi.setFcode(temp);
			offi.setFid(temp);
			offi.setFleaf("1");
		} else {
			offi = officeService.getOfficeByCode(fcode);
		}
		offi.setFadd(fadd);
		offi.setFadmin(fadmin);
		offi.setFadtel(fadtel);
		offi.setFbz("");
		offi.setFcheck("");
		offi.setFemail("");
		offi.setFinuse("1");
		offi.setFname(fname);
		offi.setForder(1);
		offi.setFparent(fparent);
		offi.setFpym("");
		offi.setFstate("1");
		offi.setFtel(ftel);
		offi.setFtype("");
		offi.setFxzqh("");
		
		//保存签约患者
		boolean boo1 = officeService.saveOffice(offi);
		
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: replaceBlank 
	* @Description: 替换回车和空格
	* @param @param str
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	private String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
	
	private String query;
	
	//树节点id
	private String node;
	
	private int limit = 50; //分页结束
	private int start = 0;  //分页起始
	private int total;		//总页数
	
	private String fbh;
	private String fname;
	private String fleaf;
	private String fparent;
	private String fdesc;
	private String fflag;
	private String ftype;

	private String fid;
	private String fcode;
	private String fpym;
	private String fadd;
	private String fadmin;
	private String fadtel;
	private String ftel;
	private String femail;
	private String fxzqh;


	public String getFid() {
		return fid;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

	public String getFpym() {
		return fpym;
	}

	public void setFpym(String fpym) {
		this.fpym = fpym;
	}

	public String getFadd() {
		return fadd;
	}

	public void setFadd(String fadd) {
		this.fadd = fadd;
	}

	public String getFadmin() {
		return fadmin;
	}

	public void setFadmin(String fadmin) {
		this.fadmin = fadmin;
	}

	public String getFadtel() {
		return fadtel;
	}

	public void setFadtel(String fadtel) {
		this.fadtel = fadtel;
	}

	public String getFtel() {
		return ftel;
	}

	public void setFtel(String ftel) {
		this.ftel = ftel;
	}

	public String getFemail() {
		return femail;
	}

	public void setFemail(String femail) {
		this.femail = femail;
	}

	public String getFxzqh() {
		return fxzqh;
	}

	public void setFxzqh(String fxzqh) {
		this.fxzqh = fxzqh;
	}

	public String getQuery() {
		return query;
	}


	public void setQuery(String query) {
		this.query = query;
	}


	public String getNode() {
		return node;
	}


	public void setNode(String node) {
		this.node = node;
	}


	public int getLimit() {
		return limit;
	}


	public void setLimit(int limit) {
		this.limit = limit;
	}


	public int getStart() {
		return start;
	}


	public void setStart(int start) {
		this.start = start;
	}


	public int getTotal() {
		return total;
	}


	public void setTotal(int total) {
		this.total = total;
	}


	public String getFbh() {
		return fbh;
	}


	public void setFbh(String fbh) {
		this.fbh = fbh;
	}


	public String getFname() {
		return fname;
	}


	public void setFname(String fname) {
		this.fname = fname;
	}


	public String getFleaf() {
		return fleaf;
	}


	public void setFleaf(String fleaf) {
		this.fleaf = fleaf;
	}


	public String getFparent() {
		return fparent;
	}


	public void setFparent(String fparent) {
		this.fparent = fparent;
	}


	public String getFdesc() {
		return fdesc;
	}


	public void setFdesc(String fdesc) {
		this.fdesc = fdesc;
	}


	public String getFflag() {
		return fflag;
	}


	public void setFflag(String fflag) {
		this.fflag = fflag;
	}


	public String getFtype() {
		return ftype;
	}


	public void setFtype(String ftype) {
		this.ftype = ftype;
	}


	public String getFcode() {
		return fcode;
	}


	public void setFcode(String fcode) {
		this.fcode = fcode;
	}
	
	
}
